<?php

/**
 * Name: 值班班长日志仓储层
 * User: Silent
 * Date: 2025-11-24
 */

namespace App\Repositories\Checkin;

use App\Models\Checkin\DutyLeaderLog;
use App\Repositories\BaseRepository;

class DutyLeaderLogRepository extends BaseRepository
{
    public function __construct(DutyLeaderLog $model)
    {
        parent::__construct($model);
    }

    /**
     * 创建值班班长日志
     */
    public function createLog($dutyLeaderId, $employeeId, $teamName, $dutyDate)
    {
        return $this->create([
            'duty_leaders_id' => $dutyLeaderId,
            'employees_id' => $employeeId,
            'team_name' => $teamName,
            'duty_date' => $dutyDate,
        ]);
    }

    /**
     * 获取指定值班班长的日志记录
     */
    public function getLogsByDutyLeaderId($dutyLeaderId)
    {
        return $this->query()
            ->where('duty_leaders_id', $dutyLeaderId)
            ->with('employee')
            ->orderBy('created_at', 'desc')
            ->get();
    }

    /**
     * 获取指定日期的日志记录
     */
    public function getLogsByDate($date)
    {
        return $this->query()
            ->where('duty_date', $date)
            ->with(['dutyLeader', 'employee'])
            ->orderBy('created_at', 'desc')
            ->get();
    }

    /**
     * 获取指定员工的排班日志
     */
    public function getLogsByEmployeeId($employeeId, $perPage = 15, $filters = [])
    {
        $query = $this->query()->where('employees_id', $employeeId);

        // 日期筛选
        if (isset($filters['start_date'])) {
            $query->where('duty_date', '>=', $filters['start_date']);
        }
        if (isset($filters['end_date'])) {
            $query->where('duty_date', '<=', $filters['end_date']);
        }

        return $query->with('dutyLeader')
            ->orderBy('duty_date', 'desc')
            ->paginate($perPage);
    }

    /**
     * 获取排班日志列表（分页）
     */
    public function getLogsList($perPage = 15, $filters = [])
    {
        $query = $this->query();

        // 日期筛选
        if (isset($filters['start_date'])) {
            $query->where('duty_date', '>=', $filters['start_date']);
        }
        if (isset($filters['end_date'])) {
            $query->where('duty_date', '<=', $filters['end_date']);
        }

        // 班组筛选
        if (isset($filters['team_name'])) {
            $query->where('team_name', $filters['team_name']);
        }

        // 员工筛选
        if (isset($filters['employees_id'])) {
            $query->where('employees_id', $filters['employees_id']);
        }

        return $query->with(['dutyLeader', 'employee'])
            ->orderBy('created_at', 'desc')
            ->paginate($perPage);
    }
}
